package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.entity.AddressBook;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.AddressBookMapper;
import com.sky.mapper.OrderDetailMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.OrderService;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrdersVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    OrderMapper orderMapper;
    @Autowired
    AddressBookMapper addressBookMapper;
    @Autowired
    ShoppingCartMapper shoppingCartMapper;
    @Autowired
    OrderDetailMapper orderDetailMapper;

    /**
     * 查询订单详情
     * @param id
     * @return
     */
    @Override
    public OrdersVo getById(Long id) {
        OrdersVo ordersVo = new OrdersVo();
        Orders result = orderMapper.getById(id);
        List<OrderDetail> listResult = orderDetailMapper.getById(id);
        BeanUtils.copyProperties(result, ordersVo);
        ordersVo.setOrderDetailList(listResult);
        return ordersVo;
    }

    /**
     * 用户下单
     *
     * @param dto
     */
    @Override
    @Transactional
    public OrderSubmitVO submit(OrdersSubmitDTO dto) {
        Long userId = BaseContext.getCurrentId();
        System.out.println(userId);
        //先判断当前订单地址是否存在
        AddressBook addressBook = addressBookMapper.getById(dto.getAddressBookId());
        if (addressBook == null) {
            throw new RuntimeException("请选择地址后下单");
        }
        //判断购物车是否有东西
        List<ShoppingCart> shoppingCart = shoppingCartMapper.getById(userId);
        if (shoppingCart == null && shoppingCart.size() == 0) {
            throw new RuntimeException("请添加购物车后下单");
        }
        //构建订单
        Orders orders = new Orders();
        String number = UUID.randomUUID().toString();
        orders.setNumber(number);
        orders.setStatus(1);
        orders.setUserId(userId);
        orders.setAddressBookId(addressBook.getId());
        orders.setCheckoutTime(LocalDateTime.now());
        orders.setOrderTime(LocalDateTime.now());
        orders.setPayMethod(dto.getPayMethod());
        orders.setAmount(dto.getAmount());
        orders.setRemark(dto.getRemark());
        orders.setPhone(addressBook.getPhone());
        orders.setAddress("山东省济南市历下区普利商务中心");
        orders.setConsignee(addressBook.getConsignee());
        orders.setEstimatedDeliveryTime(dto.getEstimatedDeliveryTime());
        orders.setDeliveryStatus(dto.getDeliveryStatus());
        orders.setPackAmount(dto.getPackAmount());
        orders.setTablewareNumber(dto.getTablewareNumber());
        //未支付
        orders.setPayStatus(0);
        orders.setTablewareStatus(dto.getTablewareStatus());
        orderMapper.save(orders);
        List<OrderDetail> result = shoppingCart.stream().map(shoppingCart1 -> {
            OrderDetail orderDetail = new OrderDetail();
            BeanUtils.copyProperties(shoppingCart1, orderDetail);
            orderDetail.setOrderId(orders.getId());
            return orderDetail;
        }).collect(Collectors.toList());
        orderDetailMapper.save(result);
        shoppingCartMapper.delete(userId);
        OrderSubmitVO orderSubmitVO = new OrderSubmitVO();
        orderSubmitVO.setId(orders.getId());
        orderSubmitVO.setOrderTime(orders.getOrderTime());
        orderSubmitVO.setOrderAmount(orders.getAmount());
        orderSubmitVO.setOrderNumber(orders.getNumber());
        return orderSubmitVO;
    }

    /**
     * 查询订单
     *
     * @param dto
     */
    @Override
    public void cancel(OrdersCancelDTO dto) {

    }


}
